#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int ans = 0; // 计数

// 折半查找
bool BSearch(vector<int> vec, int num)
{
    int low = 0, high = vec.size() - 1, mid;
    while (low <= high)
    {
        mid = (low + high) >> 1;
        if (vec[mid] == num)
        {
            ans++;
            int i = 1;
            while (mid + i < vec.size() && vec[mid + i] == num)
                i++, ans++;

            i = 1;
            while (mid - i > 0 && vec[mid - i] == num)
                i++, ans++;

            return true;
        }
        else if (vec[mid] < num)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return false;
}

void Solution(vector<int> vec, int C)
{
    for (int num : vec)
        BSearch(vec, num + C);
}

int main()
{
    int N, C;
    cout << "请输入整数数量N与差值C："; // 5 3
    cin >> N >> C;

    vector<int> vec;
    cout << "请输入" << N << "个整数：" << endl; // 2 1 4 2 5
    int i, tmp;
    for (i = 0; i < N; i++)
    {
        cin >> tmp;
        vec.push_back(tmp);
    }

    // 首先对数组进行排序
    // sort的平均时间复杂度为O(nlogn)
    std::sort(vec.begin(), vec.end());
    ans = 0;
    Solution(vec, C);

    cout << "ans=" << ans << endl; // 3

    return 0;
}